<?php
require_once '../application/modules/admin/models/Clients.php';
require_once '../application/modules/admin/models/Groups.php';
require_once '../application/modules/admin/models/Businesses.php';
require_once 'Applications.php';
require_once 'LoanProducts.php';
require_once 'Loans.php';

class Loan_BusinessController extends HTech_Controller_Action
{
    function init()
    {
        $cfg = array(
            'model' => 'Applications', 
            'form' => 'Emms_Form_Loan_Business',
            'config' => array('new' => 'loans'), // already set as default table in the model
            'redirect' => array('view','application','loan'),
            'flashMessenger' => true
        );
        parent::init($cfg);   
    }
    
    public function selectAction()
    {       
        $loans = new Loans();
        $this->view->loan = $loans->find((int) $this->_request->getParam('loan'))->current(); 
        
        if (!$this->view->loan) {
            $this->_helper->flashMessenger->addMessage($this->view->translate('Invalid business selection request'));
            $this->redirect('/default/auth/deny'); 
        }
        $this->view->form = new $this->_cfg['form']($this->view->loan);
        
        if ($this->getRequest()->isPost()) {
            if ($this->view->form->isValid($this->getRequest()->getPost())) {
                $data = $this->view->form->getValues();
                $businesses = new Businesses();
                $data['BusinessTypeId'] = $businesses->find($data['BusinessId'])->current()->TypeId;
                $loans = new Loans(array('name' => 'loans'));
                $loans->save($data);
                $this->redirect(sprintf('/loan/application/view/id/%s',$this->view->loan->LoanMasterId)); 
            }
        }
        $this->_helper->layout->setLayout('survey');
    }
}
